
#include "Object3D.h"


namespace soft_renderer
{
	Object3D::Object3D()
	{
	}

	void Object3D::setTransform(Vector3 position, Quaternion quaternion, Vector3 scale)
	{
		_position = position;
		_quaternion = quaternion;
		_scale = scale;
		// _transformMatrix.compose(_position, _quaternion, _scale);
	}

	void Object3D::setPosition(Vector3 position)
	{
		_position = position;
	}

	void Object3D::setQuaternion(Quaternion quaternion)
	{
		_quaternion = quaternion;
	}

	void Object3D::setScale(Vector3 scale)
	{
		_scale = scale;
	}

	// https://blog.csdn.net/houdy/article/details/154619
	// https://zhuanlan.zhihu.com/p/66669463
	// https://blog.csdn.net/u012419410/article/details/42174839
	void Object3D::updateTransformMatrix()
	{
		_transformMatrix.compose(_position, _quaternion, _scale);
		if (_scale.x() == _scale.y() && _scale.x() == _scale.y())
		{
			// 正交变换
			_normalMatrix.compose(Vector3(0, 0, 0), _quaternion, Vector3(1, 1, 1));
		}
		else
		{
			_normalMatrix.compose(Vector3(0, 0, 0), _quaternion, _scale);
			_normalMatrix.invert();
			_normalMatrix.transpose();
			// 这种情况下，需要对法向量归一化
		}
	}

	const Matrix4& Object3D::transformMatrix() const
	{
		return _transformMatrix;
	}

	const Matrix4& Object3D::normalMatrix() const
	{
		return _normalMatrix;
	}
}

